﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormTest.utils
{
    public class GenerateProdCode
    {
        private static readonly Random random = new Random();
        private const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

        public static string Generate(string middlePart)
        {
            if (middlePart.Length != 4 || middlePart.Any(c => !chars.Contains(c)))
                throw new ArgumentException("中间部分必须是4位大写字母或数字");

            // 生成前5位随机字符
            var prefix = new string(Enumerable.Repeat(chars, 5)
                .Select(s => s[random.Next(s.Length)]).ToArray());

            // 生成后9位随机字符，排除中间4位字符
            var validChars = chars.Except(middlePart).ToArray();
            var suffix = new string(Enumerable.Range(0, 9)
                .Select(_ => validChars[random.Next(validChars.Length)]).ToArray());

            return $"{prefix}{middlePart}{suffix}";
        }

    }
}
